package com.uyayo.notepad.service.impl;

import com.uyayo.framework.common.exception.ParentNotFoundException;
import com.uyayo.framework.core.service.impl.BaseServiceImpl;
import com.uyayo.notepad.entity.NoteCategory;
import com.uyayo.notepad.mapper.NoteCategoryMapper;
import com.uyayo.notepad.model.NoteCategoryCondition;
import com.uyayo.notepad.service.NoteCategoryService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;


@Service("noteCategoryService")
public class NoteCategoryServiceImpl extends BaseServiceImpl<NoteCategory> implements NoteCategoryService {

	private final NoteCategoryMapper noteCategoryMapper;

	public NoteCategoryServiceImpl(NoteCategoryMapper noteCategoryMapper) {
		this.noteCategoryMapper = noteCategoryMapper;
	}

	@Override
	public List<NoteCategory> selectNoteCategory(NoteCategoryCondition noteCategoryCondition) {
		Example example = new Example(NoteCategory.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo(noteCategoryCondition);
		example.createCriteria();
		return noteCategoryMapper.selectByExample(example);
	}

	@Override
	public int selectCountByParentId(Long parentId) {
		NoteCategory noteCategory = new NoteCategory();
		noteCategory.setParentId(parentId);
		return noteCategoryMapper.selectCount(noteCategory);
	}

	@Override
	public int saveNoteCategory(NoteCategory noteCategory) throws ParentNotFoundException {
		Long parentId = noteCategory.getParentId();
		if(!Objects.isNull(parentId)){
			NoteCategory noteCategoryParent = selectByKey(parentId);
			if(noteCategoryParent==null){
				throw new ParentNotFoundException("Parent not found!");
			}
		}
		int count = noteCategoryMapper.insertSelective(noteCategory);
		if(count >0){
			updateHasChildrennToParent(parentId);
		}
		return count;
	}

	/**
	 * 迭代检查选择的上级id是否是自己的子级
	 * @param parentIdOld 原上级id
	 * @param parentIdNew	新上级id
	 * @param noteCategoryChild 子资源
	 * @param noteCategoryParent 父资源
	 */
	private void recursiveCheckParentIsChildren(Long parentIdOld, Long parentIdNew, NoteCategory noteCategoryChild, NoteCategory noteCategoryParent){
		if (selectCountByParentId(noteCategoryChild.getId()) > 0) {
			Example example = new Example(NoteCategory.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("parentId",noteCategoryChild.getId());
			List<NoteCategory> noteCategorys = noteCategoryMapper.selectByExample(example);
			if (!noteCategorys.isEmpty()) {
				for(NoteCategory noteCategory:noteCategorys){
					if(Objects.equals(noteCategory.getId(),parentIdNew)){
						noteCategoryParent.setParentId(parentIdOld);
						noteCategoryMapper.updateByPrimaryKeySelective(noteCategoryParent);
					}else {
						recursiveCheckParentIsChildren(parentIdOld, parentIdNew, noteCategory, noteCategoryParent);
					}
				}
			}
		}
	}
	@Override
	public int updateNoteCategory(NoteCategory noteCategory) throws ParentNotFoundException {

		NoteCategory noteCategoryOld = selectByKey(noteCategory.getId());
		if(noteCategory.getParentId()==null){
			noteCategory.setParentId(0L);
		}
		Long parentId = noteCategory.getParentId();
		if(parentId!=0){

			//获取修改的父级资源
			NoteCategory noteCategoryParent = selectByKey(parentId);
			if(noteCategoryParent==null){
				throw new ParentNotFoundException("Parent not found!");
			}
			if (selectCountByParentId(noteCategoryOld.getId()) > 0) {
				Example example = new Example(NoteCategory.class);
				Example.Criteria criteria = example.createCriteria();
				criteria.andEqualTo("parentId",noteCategoryOld.getId());
				List<NoteCategory> noteCategorys = noteCategoryMapper.selectByExample(example);
				if (!noteCategorys.isEmpty()) {
					for(NoteCategory noteCategory_:noteCategorys){
						if(Objects.equals(noteCategory_.getId(),parentId)){
							noteCategoryParent.setParentId(noteCategoryOld.getParentId());
							noteCategoryMapper.updateByPrimaryKeySelective(noteCategoryParent);
						}else{
							recursiveCheckParentIsChildren(noteCategoryOld.getParentId(),noteCategory.getParentId(),noteCategory,noteCategoryParent);
						}
					}
				}
			}
		}
		if(selectCountByParentId(noteCategory.getId())>0) {
			noteCategory.setHasChildren(true);
		}else{
			noteCategory.setHasChildren(false);
		}
		int count = noteCategoryMapper.updateByPrimaryKeySelective(noteCategory);
		if(count<=0){
			return count;
		}
		updateHasChildrennToParent(parentId);
		if(noteCategoryOld!=null){
			updateHasChildrennToParent(noteCategoryOld.getParentId());
		}
		return count;
	}

	/**
	 * 修改父资源是否含子资源
	 * @param parentId 父资源id
	 */
	private void  updateHasChildrennToParent(Long parentId){
		if(!Objects.isNull(parentId)){
			NoteCategory noteCategory = new NoteCategory();
			noteCategory.setId(parentId);
			if(selectCountByParentId(parentId)>0) {
				noteCategory.setHasChildren(true);
			}else{
				noteCategory.setHasChildren(false);
			}
			noteCategoryMapper.updateByPrimaryKeySelective(noteCategory);
		}
	}

	@Override
	public int deleteNoteCategory(Long id) {

		if (selectCountByParentId(id) > 0) {
			Example example = new Example(NoteCategory.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("parentId",id);
			List<NoteCategory> noteCategorys = noteCategoryMapper.selectByExample(example);
			if (!noteCategorys.isEmpty()) {
				for(NoteCategory noteCategory:noteCategorys){
					recursiveDelete( noteCategory);
				}
			}
		}
		return delete(id);
	}

	/**
	 * 递归删除资源
	 * @param noteCategory 资源
	 */
	private void recursiveDelete(NoteCategory noteCategory){

		if (selectCountByParentId(noteCategory.getId()) > 0) {
			Example example = new Example(NoteCategory.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("parentId",noteCategory.getId());
			List<NoteCategory> noteCategorys = noteCategoryMapper.selectByExample(example);
			if (!noteCategorys.isEmpty()) {
				for(NoteCategory noteCategoryChild:noteCategorys){
					recursiveDelete(noteCategoryChild);
				}
			}
		}
		delete(noteCategory.getId());
	}

	public int delete(Long id) {
		NoteCategory noteCategory = noteCategoryMapper.selectByPrimaryKey(id);
		int count = noteCategoryMapper.deleteByPrimaryKey(id);
		if(count<=0){
			return count;
		}
		updateHasChildrennToParent(noteCategory!=null?noteCategory.getParentId():null);
		return count;
	}
}
